var fileLoad = [];
var flag=true;
function imgChange(obj1, obj2) {
    //获取点击的文本框
    var file = document.getElementById("file");
    //存放图片的父级元素
    var addBox = document.getElementsByClassName(obj1)[0];
    //获取的图片文件
    var fileList = file.files;
    [].push.apply(fileLoad, fileList);
    //图片的父级元素
    var box = document.getElementsByClassName(obj2)[0];
    var imgArr = [];
    //遍历获取到得图片文件
    for (var i = 0; i < fileList.length; i++) {
        var imgUrl = window.URL.createObjectURL(file.files[i]);
        imgArr.push(imgUrl);
        var img = document.createElement("img");
        img.setAttribute("src", imgArr[i]);
        img.setAttribute('data-name',fileList[i].name);
        box.insertBefore(img, addBox);
    }
    imgRemove();
}
function init() {
    var $ = layui.jquery;
    var data = new FormData();
    for (var i = 0; i < fileLoad.length; i++) {
        data.append('file[' + i + ']', fileLoad[i]);
    }
    //获取连接上的id
    readUrlToParams();
    data.append('pk_id', paraObj.pk_id);
    $.ajax({
        url: Interface.UpdateCarNoInfo,
        type: "POST",
        data: data,
        processData: false,
        contentType: false,
        success: function (data) {
            layer.msg(data.msg);
            if(data.error==0){
                window.history.back();
            }
        },
        error: function (xhr) {
            console.log("error");
            console.log(xhr);
        }
    })
}
function imgRemove() {
    var imgList = document.getElementsByClassName("imgs")[0].getElementsByTagName("img");
    for (var j = 0; j < imgList.length; j++) {
        imgList[j].index = j;
        imgList[j].onclick = function () {
            var t = this;
            var index=layer.confirm('确定要删除这张图片吗？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                var name = t.getAttribute('data-name');
                for (var i = 0; i < fileLoad.length; i++) {
                    if(fileLoad[i].name==name){
                        fileLoad.splice(i,1);
                    }
                }
                t.parentNode.removeChild(t);
                t=null;
                layer.close(index);
            }, function(){
                layer.close(index);
            });
        }
    }
}